﻿/*
 * OpenDoPE Word Add-In
 * Copyright (C) 2010-2011  Plutext Pty Ltd
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * For details, see http://www.gnu.org/licenses/gpl-2.0.html
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NLog;

using Office = Microsoft.Office.Core;


namespace OpenDoPE_Wed
{
    public partial class FormQuestion : Form
    {
        static Logger log = LogManager.GetLogger("OpenDoPE_Wed");

        private Office.CustomXMLPart questionsPart;

        private question q;

        public FormQuestion(Office.CustomXMLPart questionsPart, string xpath)
        {
            InitializeComponent();
            this.questionsPart = questionsPart;

            this.textBoxXPath.Text = xpath;
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.textBoxQID.Text)
                || string.IsNullOrWhiteSpace(this.textBoxQText.Text))
            {
                Mbox.ShowSimpleMsgBoxError("Required data missing!");
                return;
            }

            q = new question();
            q.id = this.textBoxQID.Text;
            questionText qt = new questionText();
            qt.Value = this.textBoxQText.Text;
            q.text = qt;

            // Responses
            response responses = new response();
            q.response = responses;

            if (this.radioButtonMCYes.Checked)
            {
                // MCQ: display response form
                responseFixed responseFixed = new responseFixed();
                responses.Item = responseFixed;
                FormResponses formResponses = new FormResponses(responseFixed);
                formResponses.ShowDialog();

                // TODO - handle cancel
            }
            else
            {
                // Not MCQ
                responseFree responseFree = new responseFree();
                responses.Item = responseFree;

                // Free - just text for now
                // later, configure type
                responseFree.format = responseFreeFormat.text;
            }

            // Finally, add to part
            //updateQuestionsPart(q);

            this.Close();

        }

        public question getQuestion()
        {
            return q;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Mbox.ShowSimpleMsgBoxError("Not implemented yet!");
        }

        public void updateQuestionsPart(question q)
        {

            questionnaire questionnaire = new questionnaire();
            questionnaire.Deserialize(questionsPart.XML, out questionnaire);

            questionnaire.questions.Add(q);

            // Save it in docx
            string result = questionnaire.Serialize();
            log.Info(result);
            CustomXmlUtilities.replaceXmlDoc(questionsPart, result);

        }

    }
}
